package com.mm.mindmap.handler;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mm.common.pipeline.PipelineHandler;
import com.mm.common.pipeline.Product;
import com.mm.common.utils.SessionHolder;
import com.mm.mindmap.domain.dto.MindMapDTO;
import com.mm.mindmap.domain.po.Record;
import com.mm.mindmap.mapper.RecordMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;


@Component
@RequiredArgsConstructor
public class LoadRecord extends PipelineHandler {
    private final RecordMapper recordMapper;

    @Override
    public Product process(Product product){
        MindMapDTO mindMap = product.getData(MindMapDTO.class);

        //判断用户是否登录
        if(SessionHolder.getUser() == null){
            mindMap.setIsLike(false);
            mindMap.setIsCollect(false);
            return product;
        }

        Record record = recordMapper.selectOne(new LambdaQueryWrapper<Record>()
                .eq(Record::getMid, mindMap.getId())
                .eq(Record::getUid, SessionHolder.getUser()));

        if(record == null){
            mindMap.setIsLike(false);
            mindMap.setIsCollect(false);
        }else {
            mindMap.setIsLike(record.getIsLike());
            mindMap.setIsCollect(record.getIsCollect());
        }

        return product;
    }
}
